package net.gq.logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.gq.logistics.pojo.Permission;
import net.gq.logistics.pojo.PermissionExample;
import net.gq.logistics.pojo.ResultData;
import net.gq.logistics.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ASUS
 * @date 2020/5/16
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {


    @Resource(name = "permissionService")
    private PermissionService permissionService;


    @Resource(name = "resultData")
    private ResultData rs;

    @RequestMapping("permissionPage")
    public String permissionPage() {

        return "permissionPage";
    }


    /**
     * 分页选择
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageInfo<Permission> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String keyword) {

        System.out.println(keyword + ":keys");
        PermissionExample example = new PermissionExample();

        //开始分页
        PageHelper.startPage(pageNum, pageSize);

        //关键字不为空才做条件查询
        if (StringUtils.isNotBlank(keyword)) {
            //创建限制对象
            PermissionExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%" + keyword + "%");

            PermissionExample.Criteria criteria1 = example.createCriteria();
            criteria1.andParentNameLike("%" + keyword + "%");

            example.or(criteria1);

        }


        List<Permission> permissions = permissionService.selectByExample(example);

        //创建分页信息对象
        return new PageInfo<Permission>(permissions);
    }


    /**
     * 删除页面
     *
     * @param str
     * @return
     */
    @RequestMapping("/deleteAll")
    @ResponseBody
    public ResultData delete(String str) {

        int result = 0;

        if (str != null) {
            String[] strs = str.split(",");

            for (String s : strs) {

                result = permissionService.deleteByPrimaryKey(Long.valueOf(s));
            }

            if (result > 0) {
                rs = ResultData.createRs(1, "删除成功!");
            } else {
                rs = ResultData.createRs(0, "删除失败!");
            }
        }

        return rs;

    }


    /**
     * 编辑，新增，修改页面
     *
     * @return
     */
    @RequestMapping("/edit")
    public String permissionEdit(Model model, Long permissionId) {

        System.out.println(permissionId);

        //permissionId不为空时，为修改数据
        if (permissionId != null) {

            Permission permission = permissionService.selectByPrimaryKey(permissionId);

            model.addAttribute("permission", permission);

        }


        //查询出所有父权限信息(type=menu的权限就是父权限)
        PermissionExample example = new PermissionExample();

        PermissionExample.Criteria criteria = example.createCriteria();

        criteria.andTypeEqualTo("menu");

        List<Permission> permissions = permissionService.selectByExample(example);


        model.addAttribute("permissions", permissions);

        return "permissionEdit";
    }


    /**
     * 新增权限
     *
     * @param pr
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public ResultData insert(Permission pr) {

        int result = 0;

        if (pr != null) {
            result = permissionService.insert(pr);
        }

        if (result > 0) {
            rs = ResultData.createRs(1, "插入成功！");
        } else {
            rs = ResultData.createRs(0, "插入失败！");
        }

        return rs;


    }

    /**
     * 删除权限
     *
     * @param permissionId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResultData delete(Long permissionId) {


        /*
            删除之前检查对应的权限id是否还有子权限

            查询parentId =要删除的权限Id,
            */
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(permissionId);

        List<Permission> children = permissionService.selectByExample(example);



        if ( children.size() > 0) {

            rs=ResultData.createRs(0,"此权限还存在子权限!不能删除!");

            return rs;

        }
        //存放影响集
        int result = 0;

        if (permissionId != null) {
            result = permissionService.deleteByPrimaryKey(permissionId);
        }

        if (result > 0) {
            rs = ResultData.createRs(1, "删除成功！");
        } else {
            rs = ResultData.createRs(0, "删除失败");
        }

        return rs;

    }


    @RequestMapping("/update")
    @ResponseBody
    public ResultData update(Permission permission) {

        System.out.println("开始更新...");
        System.out.println(permission.toString());
        int result = 0;

        if (permission != null) {

            result = permissionService.updateByPrimaryKey(permission);
        }

        if (result > 0) {
            rs=ResultData.createRs(1,"修改成功！");
        } else {
            rs=ResultData.createRs(0,"修改失败");
        }

        return rs;
    }


    /** 获取所有权限数据
     * @return
     */
    @RequestMapping("/getAllPermission")
    @ResponseBody
    public List<Permission> getAllPermission(){

        PermissionExample example = new PermissionExample();

        List<Permission> permissions = permissionService.selectByExample(example);

        return permissions;


    }

}
